package com.wanqing.community.manager.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.wanqing.community.bean.DepartmentBean;
import com.wanqing.community.dao.DepartmentDao;
import com.wanqing.community.manager.DepartmentManager;
import com.wanqing.community.util.CommunityException;
import com.wanqing.community.util.ConditionParse;
import com.wanqing.community.util.NumberDealTools;
import com.wanqing.community.util.StringTools;

/**
 * 部门管理的业务逻辑实现
 */
@Service("departmentManager")
public class DepartmentManagerImpl implements DepartmentManager{

	private static Logger log = Logger.getLogger(DepartmentManagerImpl.class);
	
	@Resource
	private DepartmentDao departmentDao;
	
	/**
	 * <p>Description:添加部门<p>
	 * <p>modifiContent:<p>
	 * @param department
	 * @return
	 * @throws CommunityException
	 */
	public boolean addDepartment(DepartmentBean department) throws CommunityException{		
		int i = 0;
		try{
			DepartmentBean parent = department.getParent();
			String code = this.queryDepartCode(department.getPid(), parent.getOrgCode());
			department.setOrgCode(code);
			department.setDleaf(true);			
			i = this.departmentDao.addDepartment(department);
			this.departmentDao.updateDepartmentLeaf(false, department.getPid());
		}catch (CommunityException e) {
			log.error("添加部门信息异常："+e.toString());
			throw e;
		}
		return i>0?true:false;
	}
	
	/**
	 * <p>Description:删除部门<p>
	 * <p>modifiContent:<p>
	 * @param list
	 * @return
	 * @throws CommunityException
	 */
	public boolean deleteDepartment(List<String> list) throws CommunityException{		
		int i = 0;
		try{
			i = this.departmentDao.deleteDepartment(list);
		}catch (CommunityException e) {
			log.error("删除部门信息异常："+e.toString());
			throw e;
		}
		return i>0?true:false;
	}
	
	/**
	 * <p>Description:根据条件得到部门的数量<p>
	 * <p>modifiContent:</p>
	 * @param condParse
	 * @return
	 * @throws CommunityException
	 */
	public int getDepartmentCount(ConditionParse condParse) throws CommunityException{		
		int total = 0;
		try{
			total = this.departmentDao.getDepartmentCount(condParse);
		}catch (CommunityException e) {
			log.error("根据条件得到部门的数量异常："+e.toString());
			throw e;
		}
		return total;
	}
	
	/**
	 * <p>Description:根据查询条件查询部门信息<p>
	 * <p>modifiContent:</p>
	 * @param condParse
	 * @return
	 * @throws CommunityException
	 */
	public List<DepartmentBean> queryDepartment(ConditionParse parse) throws CommunityException{
		List<DepartmentBean> list = null;
		try{			
			list = this.departmentDao.queryDepartment(parse);
		}catch (CommunityException e) {
			log.error("根据查询条件查询部门信息异常："+e.toString());
			throw e;
		}
		return list;
	}
	
	/**
	 * <p>Description:修改部门<p>
	 * <p>modifiContent:<p>
	 * @param department
	 * @return
	 * @throws CommunityException
	 */
	public boolean updateDepartment(DepartmentBean department) throws CommunityException{		
		int i = 0;
		try{			
			i = this.departmentDao.updateDepartment(department);
		}catch (CommunityException e) {
			log.error("修改部门信息异常："+e.toString());
			throw e;
		}
		return i>0?true:false;
	}
	
	/**
	 * <p>Description:修改部门的是否为根<p>
	 * <p>modifiContent:<p>
	 * @param dleaf
	 * @param id
	 * @return
	 * @throws CommunityException
	 */
	public boolean updateDepartmentLeaf(boolean dleaf, String id) throws CommunityException{		
		int i = 0;
		try{			
			i = this.departmentDao.updateDepartmentLeaf(dleaf, id);
		}catch (CommunityException e) {
			log.error("修改部门信息异常："+e.toString());
			throw e;
		}
		return i>0?true:false;
	}
	
	/**
	 * <p>Description:创建部门的编码<p>
	 * <p>modifiContent:<p>
	 * @param parentId
	 * @param parentCode
	 * @return
	 */
	private String queryDepartCode(String parentId,String parentCode){
		ConditionParse condParse = new ConditionParse();
		condParse.addCondition("PID", "=", parentId);		
		condParse.addWhereStr();
		String pcode = StringTools.objectToString(parentCode);
		int total = this.getDepartmentCount(condParse);
		int forObj = total+1;		
		String temp = NumberDealTools.format(forObj, 3);		
		String result = pcode+temp;			
		return result;
	}

	/**
	 * <p>Description:采用递归删除部门以及其下属部门<p>
	 * <p>modifiContent:<p>
	 * @param orgId
	 * @return
	 * @throws CommunityException
	 */
	public boolean deleteDepartment(String orgId) throws CommunityException {
		int i = 0;
		try{
			ConditionParse condParse = new ConditionParse();
			condParse.addCondition("ID", "=", orgId);
			condParse.addWhereStr();
			List<DepartmentBean> list = this.departmentDao.queryDepartment(condParse);
			String pid = "";
			if(null!=list && !list.isEmpty()){
				DepartmentBean db = list.get(0);
				pid = db.getPid();				
			}
			i = this.departmentDao.deleteDepartment(orgId);
			if(i>0 && !StringTools.isEmptyOrNull(pid)){
				condParse.deleteAll();
				condParse.addCondition("PID", "=", pid);
				condParse.addWhereStr();
				int total = this.departmentDao.getDepartmentCount(condParse);
				if(total==0){//当前节点的上级节点没有子节点了									
					this.departmentDao.updateDepartmentLeaf(true, pid);
				}
			}
		}catch (CommunityException e) {
			log.error("删除部门信息异常："+e.toString());
			throw e;
		}
		return i>0?true:false;
	}

	/**
	  * <p>Description:根据ID查找部门<p>
	  * <p>modifiContent:<p>
	  * @param id
	  * @return
	  * @throws CommunityException
	  */
	public DepartmentBean findDepartmentById(String dId) throws CommunityException 
	{		
		DepartmentBean department = new DepartmentBean();
		try 
		{
			department = this.departmentDao.findDepartmentById(dId);
		} 
		catch (CommunityException e) 
		{
			log.error("根据ID查找部门异常！"+e.toString());
			throw e;
		}
		return department;
	}
	
}
